﻿using System;
using System.Linq;

namespace Tyshka.Subroutines.Kernel.Extensions
{
    public static class ArraysExtension
    {
        public static byte[] MergeByteArrays(params byte[][] arrays)
        {
            Int32 totalArrayLength = arrays.Sum(current => current.Length);

            var result = new byte[totalArrayLength];

            int offset = 0;
            foreach (var array in arrays)
            {
                Array.Copy(array, 0, result, offset, array.Length);
                offset += arrays.Length;
            }

            return result;
        }

        public static byte[] MergeByteArrays(this byte[] source, params byte[][] arrays)
        {
            Int32 totalArrayLength = source.Length + arrays.Sum(current => current.Length);

            var result = new byte[totalArrayLength];
            int offset = source.Length;
            Array.Copy(source, 0, result, 0, source.Length);
            foreach (var array in arrays)
            {
                Array.Copy(array, 0, result, offset, array.Length);
                offset += arrays.Length;
            }
            return result;
        }
    }
}
